package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;
//    private static Logger log = LoggerFactory.getLogger(DeptController.class);
    @GetMapping
    public Result getList(){
        List<Dept>list=deptService.getList();
        log.info("查询全部部门");
        return Result.success(list);
    }

    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable int id) {
        log.info("根据id{}删除部门",id);
        deptService.deleteById(id);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("新增部门：{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable int id){
        return deptService.getById(id);
    }

    @Log
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        deptService.updateDept(dept);
        return Result.success();
    }
}
